package cc.wenshixin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cc.wenshixin.entity.Customer;
import cc.wenshixin.entity.Level;
import cc.wenshixin.entity.PageBean;
import cc.wenshixin.service.CustomerService;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	//属性注入
	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	//模型驱动封装
	private Customer customer = new Customer();
	@Override
	public Customer getModel() {
		return customer;
	}
	
	//属性封装
	private Integer currentPage;
/*	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}*/
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	//转到添加页面
	public String toAddPage() {
		List<Level> levelList = customerService.findAllLevel();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("levelList", levelList);
		return "toAddPage";
	}
	
	//添加的方法
	public String add() {
		customerService.add(customer);
		return "add";
	}
	
	//列表显示的方法
	public String list() {
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("customerList", customerList);
		return "list";
	}
	
	//转到修改页面
	public String toEditPage() {
		int id = customer.getId();
		Customer c = customerService.findOne(id);
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Level> levelList = customerService.findAllLevel();
		request.setAttribute("levelList", levelList);
		request.setAttribute("customer", c);
		return "toEditPage";
	}
	
	//修改的方法
	public String update() {
		customerService.update(customer);
		return "update";
	}
	
	//删除的方法
	public String delete() {
		int id = customer.getId();
		Customer c = customerService.findOne(id);
		if(c != null) {
			customerService.delete(c);
		}
		return "delete";
	}
	
	//有条件的查询
	public String condition() {
		if(customer.getName() != null && !"".equals(customer.getName())) {
			List<Customer> customerList = customerService.findCondition(customer);
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("customerList", customerList);
		} else {
			list();
		}
		return "condition";
	}
	
	//分页的显示方法
	public String listPage() {
		PageBean pageBean = customerService.listPage(currentPage);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("pageBean", pageBean);
		return "listPage";
	}
	
	//转到查询页面
	public String toSelectPage() {
		return "toSelectPage";
	}
	
	
	//查询的方法
	public String select() {
		List<Customer> customerList = customerService.selectMoreCondition(customer);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("customerList", customerList);
		return "select";
	}
	
	//客户级别统计的方法
	public String countLevel() {
		List countLevels = customerService.findCountLevel();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("countLevels", countLevels);
		return "countLevel";
	}
	
	//客户来源统计的方法
	public String countSource() {
		List countSources = customerService.findCountSource();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("countSources", countSources);
		return "countSource";
	}
}
